package wpRelations;

import java.util.ArrayList;


import processing.core.PApplet;


public class WiPhi_001 extends PApplet {

	/**
	 * @param args
	 */
	int[][] cTable;
	int[][] lTable;
	wArticle personZero;
	String personZeroTitle = "Coby_Miller";
	ArrayList<wCategory> consolidatedSuperCats;
	ArrayList<wArticle> links;
	//Matrix cMatrix;
	BinaryTable bt;
	int[][] data = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
	TsvTable myTable;
	
	public void setup() {
		ArrayList<String> consolidatedSuperCatTitles = new ArrayList();
		ArrayList<String> consolidatedLinks = new ArrayList();
		links = new ArrayList();
		consolidatedSuperCats = new ArrayList();
		personZero = new wArticle(this, personZeroTitle);
		links = personZero.getLinks();
		links.add(0, personZero);
		int counter = 0;
		for (int i = 0; i < links.size(); i++) {
			
			wArticle wa = links.get(i);
			if (!consolidatedLinks.contains(wa.title)) consolidatedLinks.add(wa.title);
			ArrayList<wCategory> wc = wa.getCategories();
			for (int j = 0; j < wc.size(); j++) {
				wCategory wc2 = wc.get(j);
				
				if(!consolidatedSuperCatTitles.contains(wc2.title)) {
					consolidatedSuperCatTitles.add(wc2.title);
					consolidatedSuperCats.add(wc2);
				}
				counter++;
				//System.out.println(wc2.title);
			}
		}
		
		System.out.println("all supercategories for " + personZeroTitle+ " : " + consolidatedSuperCats.size());
		System.out.println("total: " + counter);
		System.out.println("consolidated links: " + consolidatedLinks.size());
		
//		cTable = new int[links.size()][consolidatedSuperCatTitles.size()];
//		for (int i = 0; i < cTable.length; i++) {
//			ArrayList<String> wct =links.get(i).getCategoryTitles(); 
//			for (int j = 0; j < cTable[0].length; j++) {
//				if (wct.contains(consolidatedSuperCats.get(j).title)) {
//					cTable[i][j] = 1;
//				} else {
//					cTable[i][j] = 0;
//				}
//			}
//		}
//		
//		lTable = new int[links.size()][links.size()];
//		for (int i = 0; i < lTable.length; i++) {
//			ArrayList<String> wlt =links.get(i).getLinkTitles(); 
//			for (int j = 0; j < lTable[0].length; j++) {
//				if (wlt.contains(consolidatedLinks.get(j))) {
//					lTable[i][j] = 1;
//				} else {
//					lTable[i][j] = 0;
//				}
//			}
//		}
//		
//		
//
//		TsvWriterModule twm = new TsvWriterModule(this);
//		twm.init("cTable_albert.tsv");
//		String[] s0 = new String[cTable[0].length+1];
//		s0[0] = "";
//		for (int j = 1; j < s0.length; j++) {
//			s0[j] = consolidatedSuperCatTitles.get(j-1);
//		}
//		twm.printStringRow(s0);
//		for (int i = 1; i <cTable.length; i++) {
//			System.out.println("iiii " + i);
//		String s[] = new String[cTable[0].length+1];
//		s[0] = consolidatedLinks.get(i);
//		for (int j = 1; j < s.length; j++) {
//			s[j] = ""+cTable[i][j-1];
//		}
//		twm.printStringRow(s);
//		}
//		twm.finish();
		//println(factor(60));
		//println(factor(21));	
		//println(safeLongToInt(factor(10)));
		//int[][] a = getCombination(50, 4);
		myTable = new TsvTable(this, "cTable_coby.tsv");
		bt = new BinaryTable(this, myTable);
		bt.setColoumnLabels(consolidatedSuperCatTitles);
		bt.setRowLabels(consolidatedLinks);
		bt.sortColoumns();
		bt.printToConsole();
	}
	
	public void draw() {
		
	}

	public int[][] getCombination(int n, int k) {
		long i0 = factor(n,k);
		long i1 = factor(k);
		long i2 = i0/i1;
		int[][] result = new int[safeLongToInt(i2)][safeLongToInt(k)];
		System.out.println(result.length);
		System.out.println(result[0].length);
		return result;
	}
	
	public long factor(int f) {
		long result = 1;
		for (int i = f; i>1; i--) {
			result*=i;
		}
		return result;
	}
	public long factor(int f, int m) {
		long result = 1;
		for (int i = f; i>(f-m); i--) {
			result*=i;
		}
		return result;
	}
	
	public static int safeLongToInt(long l) {
	    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
	        throw new IllegalArgumentException
	            (l + " cannot be cast to int without changing its value.");
	    }
	    return (int) l;
	}
	
	public int[][] matrixMultiply(int[][] a, int[][] b) {
		int[][] result;
		if (a[0].length != b.length) {
			return null;
		} else {
			result = new int[a[0].length][b.length];
			//for (int i = 0; i < )
		}
		
		
		return result;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
